<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="商品名称" prop="queryGoodsName">
        <el-input
          v-model="queryParams.queryGoodsName"
          placeholder="搜索商品名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="画师" prop="queryPainterName">
        <el-input
          v-model="queryParams.queryPainterName"
          placeholder="请选择画师"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="pageList"
      highlight-current-row
      @current-change="handleCurrentChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" type="index" width="100" />
      <el-table-column
        label="商品名称"
        align="center"
        width="300"
        prop="goodsName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="画师"
        align="center"
        prop="painterName"
        width="150"
      />
      <el-table-column label="转让状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="stutasList" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="商品类型"
        align="center"
        prop="type"
        width="150"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{ handleChangeType(scope.row.type) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="价格"
        align="center"
        prop="price"
        width="100"
        sortable
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Goods">
import {
  listGoods,
  addGoods,
  updateGoods,
  deleteGoods,
} from "@/api/business/goods";
import { listPainter } from "@/api/business/painter";
import { listAllOrder } from "@/api/business/order";
import { useDebounceFn } from "@vueuse/core";

const { proxy } = getCurrentInstance();
// 图片上传地址
const uploadUrl = import.meta.env.VITE_APP_UPLOAD_API + "api/uploadCos/goods";
const uploadShowUrl = import.meta.env.VITE_APP_COS_API;
const imageUrl = ref("");

const pageList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const detailTitle = ref(""); // 详情标题栏

const emit = defineEmits(["change"]);

const painterList = ref([]); // 画师下拉框
const useOtherPlatformList = ref([
  { label: "是", value: "是" },
  { label: "否", value: "否" },
]);
const statusList = ref([
  { label: "可以转让", value: 1 },
  { label: "禁止转让", value: 2 },
]);
const useRangeList = ref([
  { label: "仅文游圈作者用", value: 1 },
  { label: "仅文游圈、小说、广播短剧作者用", value: 2 },
  { label: "全网可商用", value: 3 },
]);
const typeList = ref([
  { label: "限时", value: 0 },
  { label: "限量", value: 1 },
]);
const openSub = ref(false); // 查看购买详情
const loadingSub = ref(false);
const pageListSub = ref([]);

const data = reactive({
  stutasList: [
    {
      label: "禁止转让",
      value: "2",
      elTagClass: "",
      elTagType: "danger",
    },
    {
      label: "可以转让",
      value: "1",
      elTagClass: "",
      elTagType: "success",
    },
  ],
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    queryGoodsName: undefined,
    queryPainterName: undefined,
    orderType: undefined,
    orderName: undefined,
  },
  rules: {
    name: [{ required: true, message: "商品名称不能为空", trigger: "blur" }],
    painterId: [{ required: true, message: "画师不能为空", trigger: "change" }],
    saleTime: [
      { required: true, message: "售卖日期不能为空", trigger: "change" },
    ],
  },
});

const { queryParams, form, rules, stutasList } = toRefs(data);

const beforeAvatarUpload = (rawFile) => {
  if (rawFile.type !== "image/png" && rawFile.type !== "image/jpeg") {
    ElMessage.error("Avatar picture must be PNG format!");
    return false;
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error("Avatar picture size can not exceed 2MB!");
    return false;
  }
  return true;
};
function onSuccessImg(response) {
  if (response.code == 200) {
    form.value.src = response.data;
    imageUrl.value = uploadShowUrl + response.data;
  }
}
/** 查询画师列表 */
function getPainterList() {
  // loading.value = true;
  listPainter().then((response) => {
    painterList.value = response.data.map((item) => {
      return {
        label: item.name,
        value: item.id,
      };
    });
    // loading.value = false;
  });
}

/** 查询公告列表 */
function getList() {
  loading.value = true;
  listGoods(queryParams.value).then((response) => {
    pageList.value = response.data.rows;
    total.value = response.data.count;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    name: undefined,
    painterId: undefined,
    saleTime: undefined,
    price: 0,
    saleSum: 0,
    status: 1,
    useRange: 1,
    type: 1,
    src: "",
    useOtherPlatform: "是",
  };
  imageUrl.value = null;
  proxy.resetForm("noticeRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

// 防抖操作
const debouncedFn = useDebounceFn(
  () => {
    submitForm();
  },
  1000,
  { maxWait: 3000 }
);

/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateGoods(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addGoods(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

function handleChangeType(params) {
  let arr = typeList.value.filter((item) => {
    return item.value == params;
  });
  if (arr && arr.length > 0) {
    return arr[0].label;
  } else {
    return " ";
  }
}

function handleCurrentChange(val) {
  const sendData = toRaw(val);
  emit("change", sendData);
}

getList();
getPainterList();
</script>
<style lang="scss" scoped>
.color-blue {
  color: rgb(0, 119, 255);
  cursor: pointer;
}
.avatar-uploader {
  width: 200px;
  height: 300px;
  .avatar {
    width: 100%;
    height: 100%;
  }
}
.avatar-uploader :deep(.el-upload) {
  width: 100%;
  height: 100%;
  border: 1px dashed #dcdfe6;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader :deep(.el-upload:hover) {
  border-color: #409eff;
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
